package logic

import (
	"context"
	"errors"
	"goods-login/goods/goods"
	"goods-login/goods/internal/svc"
	"goods-login/model/mysql"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsAddLogic {
	return &GoodsAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsAddLogic) GoodsAdd(in *goods.GoodsAddRequest) (*goods.GoodsAddResponse, error) {
	// todo: add your logic	 here and delete this line
	var user mysql.User
	err := user.FindUserByName(mysql.DB, in.UserId)
	if err != nil {
		return nil, errors.New("用户未登录")
	}
	data := mysql.Good{
		Name:     in.Name,
		Price:    float64(in.Price),
		Num:      in.Num,
		ShopName: in.ShopName,
	}
	err = data.GoodAdd(mysql.DB)
	if err != nil {
		return nil, err
	}
	return &goods.GoodsAddResponse{
		GoodsId: int64(data.ID),
	}, nil
}
